package com.star.sso.config.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * todo 改造成从token中获取用户信息
 * mybatisplus自动字段填充（有些底层已经填充了）
 * @author zhouhengzhe
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (getFieldValByName("createBy", metaObject) == null) {
            //后期需要改成从token中获取用户信息
            this.strictInsertFill(metaObject, "createBy", String.class,"ADMIN_CODE");
        }
        if (getFieldValByName("creator", metaObject) == null) {
            this.strictInsertFill(metaObject, "creator", String.class, "ADMIN");
        }
        if (getFieldValByName("updateBy", metaObject) == null) {
            this.strictInsertFill(metaObject, "updateBy", String.class, "ADMIN_CODE");
        }
        if (getFieldValByName("updater", metaObject) == null) {
            this.strictInsertFill(metaObject, "updater", String.class, "ADMIN");
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (getFieldValByName("updateBy", metaObject) == null) {
            this.strictUpdateFill(metaObject, "updateBy", String.class, "ADMIN_CODE");
        }
        if (getFieldValByName("updater", metaObject) == null) {
            this.strictUpdateFill(metaObject, "updater", String.class, "ADMIN");
        }
    }
}